package com.aletto.general_store.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aletto.general_store.R;
import com.aletto.general_store.presenters.SearchGoodsLinearPageTemplate;
import com.aletto.general_store.presenters.SearchGoodsPageTemplate;
import com.aletto.general_store.presenters.SearchStorePageTemplate;
import com.aletto.general_store.ui.popup.GoodsStoreSwitchPopup;
import com.aletto.general_store.utils.XHelper;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

/**
 * 搜索结果列表
 */
public class SearchDetailsActivity extends AppCompatActivity {

    public static Intent newIntent(Context context, String keyword, int searchType) {
        Intent intent = new Intent(context, SearchDetailsActivity.class);
        intent.putExtra("KEYWORD", keyword);
        intent.putExtra("SEARCH_TYPE", searchType);
        return intent;
    }


    private TextView mSwitchTextView;
    private View mViewDivide;
    private CheckBox mToggleDisplayCheckBox;
    private TextView searchKeywordText;
    private RecyclerView recyclerView;
    private SmartRefreshLayout refreshLayout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_details);

        mSwitchTextView = findViewById(R.id.switch_text);

        searchKeywordText = findViewById(R.id.tv_search_keyword);
        Intent intent = getIntent();
        if (intent != null) {
            String keyword = intent.getStringExtra("KEYWORD");
            searchKeywordText.setText(keyword);
        }

        // 跳转搜索
        searchKeywordText.setOnClickListener(v ->
                startActivity(SearchActivity.newIntent(v.getContext(), searchKeywordText.getText().toString())));
        findViewById(R.id.layout_search).setOnClickListener(v ->
                startActivity(new Intent(this, SearchActivity.class)));

        findViewById(R.id.back_image).setOnClickListener(v -> finish());

        mViewDivide = findViewById(R.id.view_divide);

        recyclerView = findViewById(com.sisyphus.spinach.R.id.recycler_view);
        refreshLayout = findViewById(com.sisyphus.spinach.R.id.refresh_layout);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);

        // 切换搜索对象
        int searchType = GoodsStoreSwitchPopup.GOODS_TYPE;
        GoodsStoreSwitchPopup popup = new GoodsStoreSwitchPopup(this);
        if (intent != null) {
            searchType = intent.getIntExtra("SEARCH_TYPE", GoodsStoreSwitchPopup.GOODS_TYPE);
            popup.setSearchType(searchType);
            mSwitchTextView.setText(searchType == GoodsStoreSwitchPopup.GOODS_TYPE ? R.string.search_goods : R.string.goods_store);
        }

        BasePopupView basePopup = new XPopup.Builder(this)
                .atView(mSwitchTextView)
                .asCustom(popup.setOnCheckedChangeListener(this::switchType));
        mSwitchTextView.setOnClickListener(v -> basePopup.show());

        // 切换显示模式
        mToggleDisplayCheckBox = findViewById(R.id.check_toggle_display);
        mToggleDisplayCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> renderList());

        // toggleDisplayButton.setChecked(false);

        // 搜索商品
       if (searchType == GoodsStoreSwitchPopup.GOODS_TYPE){
           // 显示切换样式按键
           XHelper.setVisibility(View.VISIBLE, mViewDivide, mToggleDisplayCheckBox);

           // 搜索商品
           renderList();

       } else {

           // 隐藏切换样式按键
           XHelper.setVisibility(View.GONE, mViewDivide, mToggleDisplayCheckBox);

           // 搜索店铺
           new SearchStorePageTemplate(recyclerView, refreshLayout)
                   .params(searchKeywordText.getText().toString())
                   .execute(getLifecycle());

       }

    }

    private void renderList() {
        if (mToggleDisplayCheckBox.isChecked()) {
            // 线性样式
            new SearchGoodsLinearPageTemplate(recyclerView, refreshLayout)
                    .params(searchKeywordText.getText().toString())
                    .execute(getLifecycle());

        } else {
            // 搜索商品
            new SearchGoodsPageTemplate(recyclerView, refreshLayout)
                    .params(searchKeywordText.getText().toString())
                    .execute(getLifecycle());
        }
    }

    private void switchType(RadioGroup group, @IdRes int checkedId) {
        RadioButton checkRadioButton = group.findViewById(checkedId);
        mSwitchTextView.setText(checkRadioButton.getText());

        if (checkedId == R.id.switch_goods_radio) {
            // 显示切换样式按键
            XHelper.setVisibility(View.VISIBLE, mViewDivide, mToggleDisplayCheckBox);

            // 搜索商品
            renderList();

        } else if (checkedId == R.id.switch_store_radio) {
            // 隐藏切换样式按键
            XHelper.setVisibility(View.GONE, mViewDivide, mToggleDisplayCheckBox);

            // 搜索店铺
            new SearchStorePageTemplate(recyclerView, refreshLayout)
                    .params(searchKeywordText.getText().toString())
                    .execute(getLifecycle());
        }
    }


}
